package com.sky.service.impl;

import com.sky.dto.ReportTimeDto;
import com.sky.entity.Orders;
import com.sky.mapper.OrdersMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * TODO
 *
 * @Author mr.wu
 * @Data #{DATE} #{TIME}
 */
@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 营业额统计
     *
     * @param dto
     * @return
     */
    @Override
    public TurnoverReportVO turnoverStatistics(ReportTimeDto dto) {
        LocalDate begin = dto.getBegin();
        List<LocalDate> dateList = new ArrayList<>();
        dateList.add(begin);
        while (!begin.equals(dto.getEnd())) {
            begin = begin.plusDays(1);
            dateList.add(begin);
        }
        List<Double> amountList = new ArrayList<>();
        for (LocalDate date : dateList) {
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            Map<String, Object> map = new HashMap<>();
            map.put("status", Orders.COMPLETED);
            map.put("start", beginTime);
            map.put("end", endTime);
            Double amounts = ordersMapper.sumByTime(map);
            amounts = amounts == null ? 0.0 : amounts;
            amountList.add(amounts);
        }
        TurnoverReportVO vo = new TurnoverReportVO();
        String amount = StringUtils.join(amountList, ",");
        String date = StringUtils.join(dateList, ",");
        vo.setDateList(date);
        vo.setTurnoverList(amount);
        return vo;
    }



    /**
     * 用户统计
     *
     * @param dto
     * @return
     */
    @Override
    public UserReportVO userStatistics(ReportTimeDto dto) {
        LocalDate begin = dto.getBegin();
        List<LocalDate> dateList = new ArrayList<>();
        dateList.add(begin);
        while (!begin.equals(dto.getEnd())) {
            begin = begin.plusDays(1);
            dateList.add(begin);
        }

        List<Integer> newUserList = new ArrayList<>(); //新增用户数
        List<Integer> totalUserList = new ArrayList<>(); //总用户数
        for (LocalDate date : dateList) {
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            Map<String, Object> map = new HashMap<>();
            map.put("start", beginTime);
            map.put("end", endTime);

            Integer userSum = userMapper.sumUser();
            Integer userNew = userMapper.newUser();
            newUserList.add(userNew);
            totalUserList.add(userSum);
        }
        UserReportVO vo = new UserReportVO();
        vo.setDateList(StringUtils.join(dateList, ","));
        vo.setNewUserList(StringUtils.join(newUserList, ","));
        vo.setTotalUserList(StringUtils.join(totalUserList, ","));
        return vo;
    }

    /**
     * 订单统计
     *
     * @param dto
     * @return
     */
    @Override
    public OrderReportVO ordersStatistics(ReportTimeDto dto) {
        LocalDate begin = dto.getBegin();
        List<LocalDate> dateList = new ArrayList<>();
        dateList.add(begin);
        while (!begin.equals(dto.getEnd())) {
            begin = begin.plusDays(1);
            dateList.add(begin);
        }

        //订单数列表
        List<Integer> orderCountList = new ArrayList<>();
        //订单总数
        Integer totalOrderCount = ordersMapper.totalOrder(new HashMap<>());
        //有效订单数
        Map<String, Object> completeMap = new HashMap<>();
        Integer totalOrderComplete = ordersMapper.totalOrder(completeMap);
        //有效订单数列表
        List<Integer> orderCompleteList = new ArrayList<>();
        for (LocalDate date : dateList) {
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            Map<String, Object> map = new HashMap<>();
            map.put("start", beginTime);
            map.put("end", endTime);
            Integer orderCount =  ordersMapper.selectOrder(map);
            orderCountList.add(orderCount);
            map.put("status",Orders.COMPLETED);
            Integer orderComplete =  ordersMapper.selectOrder(map);
            orderCompleteList.add(orderComplete);
        }

        OrderReportVO vo  = new OrderReportVO();
        vo.setTotalOrderCount(totalOrderCount);
        vo.setValidOrderCount(totalOrderComplete);
        Double orderCompletionRate = 0.0;
        if(totalOrderCount != 0){
            orderCompletionRate = totalOrderComplete.doubleValue() / totalOrderCount;
        }
        vo.setOrderCompletionRate(orderCompletionRate);
        vo.setDateList(StringUtils.join(dateList, ","));
        vo.setOrderCountList(StringUtils.join(orderCountList,","));
        vo.setValidOrderCountList(StringUtils.join(orderCompleteList,","));
        return vo;
    }


}
